#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" This module defines the URL namespace for OCIE.

"""
from functools import partial
from os.path import abspath, dirname, join
from tack.lib import no_cache_app, compose
from tack.lib.urlns import *
from tack_default import api as default_api
from tack_ocie.api import schedule_get_public, schedule_get, schedule_put, schedule_post, schedule_delete, schedule_search
from tack_ocie.api import site_config, common_lookups, units, appointment_types, reports, report_results


here = abspath(dirname(__file__))
static_dir = join(here, 'static')
htdoc_file = partial(join, static_dir, 'htdoc')
local404 = template_app(htdoc_file('error_404.html'), status=status_codes.not_found)


def getdsn(env):
    try:
        return env['tack.siteconfig']['ocie_dsn']
    except (KeyError, ):
        pass


# our URL DSL
DB = partial(confdbcon, dsn_lookup=getdsn)
FORM = formreader
FORMDB = compose([FORM, DB])
FILES = partial(fileserver, handle404=local404)
NC = no_cache_app
COOKIE = cookie_monster
COOKIEAUTH = default_api.cookie_auth_app
SETROLES = default_api.set_login_roles_app
NEEDROLES = default_api.require_roles_app
ADMINROLES = ['OCIE Admin']
USERROLES = ['OCIE User']


def GUARD(app, roles):
    return DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles)))))
    
def FGUARD(app, roles):
    return FORM(DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles))))))
    
def NCGUARD(app, roles):
    return NC(DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles))))))


urls = [
    ('/api/ocie/appointment_types/', 'GET', GUARD(appointment_types.get_all, USERROLES)),
    ('/api/ocie/schedule_public/(?P<start>.*)/(?P<finish>.*)/', 'GET', NC(DB(schedule_get_public.get_by_date_range))),
    ('/api/ocie/schedule/', 'PUT', FGUARD(schedule_put.add_resourced, USERROLES)),
    ('/api/ocie/schedule/(?P<schedule_id>\d+)/', 'POST', FGUARD(schedule_post.update_resourced, USERROLES)),
    ('/api/ocie/schedule/draft/', 'PUT', FGUARD(schedule_put.add_drafted, USERROLES)),
    ('/api/ocie/schedule/tasks/', 'GET', NCGUARD(schedule_get.get_tasks, USERROLES)),
    ('/api/ocie/schedule/rescheduled/(?P<schedule_id>\d+)/', 'POST', FGUARD(schedule_post.update_rescheduled, USERROLES)),
    ('/api/ocie/schedule/drafted/(?P<schedule_id>\d+)/', 'POST', FGUARD(schedule_post.update_drafted, USERROLES)),
    ('/api/ocie/schedule/canceled/', 'GET', NCGUARD(schedule_get.get_canceled, USERROLES)),
    ('/api/ocie/schedule/search/', 'GET', FGUARD(schedule_search.get, USERROLES)),
    ('/api/ocie/schedule/(?P<start>.*)/(?P<finish>.*)/', 'GET', NCGUARD(schedule_get.get_by_date, USERROLES)),
    ('/api/ocie/json/schedule/(?P<start>.*)/(?P<finish>.*)/', 'GET', NCGUARD(schedule_get.get_json_by_date, USERROLES)),
    ('/api/ocie/schedule/(?P<schedule_id>\d+)/', 'GET', NCGUARD(schedule_get.get_by_id, USERROLES)),
    ('/api/ocie/schedule/(?P<schedule_id>\d+)/', 'DELETE', GUARD(schedule_delete.cancel, USERROLES)),
    ('/api/ocie/reports/', 'PUT', FGUARD(reports.add, USERROLES)),
    ('/api/ocie/reports/', 'GET', GUARD(reports.get, USERROLES)),
    ('/api/ocie/reports/(?P<report_id>\d+)/', 'POST', FGUARD(reports.update, USERROLES)),
    ('/api/ocie/reports/(?P<report_id>\d+)/', 'DELETE', GUARD(reports.delete, USERROLES)),
    ('/api/ocie/report_results/(?P<report_id>\d+)/', 'POST', FGUARD(report_results.get, USERROLES)),
    ('/api/ocie/common_lookups/', 'GET', NCGUARD(common_lookups.get, USERROLES)),
    ('/api/ocie/units/', 'GET', GUARD(units.get_all, USERROLES)),
    ('/api/ocie/units/', 'PUT', FGUARD(units.add_unit, USERROLES)),
    ('/api/ocie/units/(?P<key>\d+)/', 'GET', GUARD(units.get_keyed, USERROLES)),    
    ('/api/ocie/units/(?P<key>\d+)/', 'POST', FGUARD(units.update_unit, USERROLES)),
    ('/api/ocie/units/(?P<key>\d+)/', 'DELETE', GUARD(units.delete_unit, USERROLES)),    
    ('/api/ocie/config_public/', 'GET', site_config.get_public),
    ('/api/ocie/config/site/', 'GET', NCGUARD(site_config.get_site, ADMINROLES)),
    ('/api/ocie/config/site/', 'POST', FGUARD(site_config.post_site, ADMINROLES)),
    ('/api/ocie/config/lists/units/', 'GET', NCGUARD(site_config.get_units, ADMINROLES)),
    ('/api/ocie/config/lists/units/', 'PUT', FGUARD(site_config.add_unit, ADMINROLES)),
    ('/api/ocie/config/lists/units/(?P<unit_id>\d+)/', 'GET', NCGUARD(site_config.get_unit, ADMINROLES)),
    ('/api/ocie/config/lists/units/(?P<unit_id>\d+)/', 'POST', FGUARD(site_config.update_unit, ADMINROLES)),
    ('/api/ocie/config/email/', 'GET', NCGUARD(site_config.get_email, ADMINROLES)),
    ('/api/ocie/config/email/', 'POST', FGUARD(site_config.post_email, ADMINROLES)),
    ('/api/ocie/role_check/', 'GET', GUARD(default_api.k_checker, ADMINROLES)), 
    ('/ocie/(?P<fname>.*)',  'GET',  FILES(static_dir)),
]
